from contextlib import asynccontextmanager
from fastapi import FastAPI

from api.api import api_router
from config.loader import get_app_config
from services.app_services import AppServices
from config.base_app_config import BaseAppConfig
from state.app_state import AppState


@asynccontextmanager
async def lifespan(app: FastAPI):
    app_config = get_app_config()

    # Setup (e.g., DB connection, services)
    app.state.app_state = AppState(
        db=app_config.DATABASE_URL, app_config=app_config)

    app.state.services = AppServices(app_state=app.state.app_state)
    yield

    # Cleanup

app = FastAPI(lifespan=lifespan)
app.include_router(api_router, prefix="/api")
